package com.finger.crowd.mvc.controller;

import com.finger.crowd.entity.Auth;
import com.finger.crowd.entity.Role;
import com.finger.crowd.service.api.AdminService;
import com.finger.crowd.service.api.AuthService;
import com.finger.crowd.service.api.RoleService;
import com.finger.crowd.util.ResultEntity;
import com.google.gson.Gson;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
public class AssignRoleController {
    @Autowired
    private RoleService roleService;
    @Autowired
    private AuthService authService;
    @Autowired
    private AdminService adminService;

    private Gson gson = new Gson();

    private static Logger logger;

    static {
        logger = LoggerFactory.getLogger(AssignRoleController.class);
    }

    @RequestMapping("/assign/to/role.html")
    public String assignRoleToAdmin(@RequestParam("adminId") Integer adminId,
                                    ModelMap map) {
        List<Role> assignedRole = roleService.selectAssignedByAdminId(adminId);
        List<Role> unAssignedRole = roleService.selectUnAssignedByAdminId(adminId);
        map.addAttribute("assignedRoles", assignedRole);
        map.addAttribute("unAssignedRoles", unAssignedRole);
        return "assign-role";
    }

    @RequestMapping("/assign/to/admin.html")
    public String addignAdmin(@RequestParam("keyword") String keyword,
                              @RequestParam("pageNum") Integer pageNum,
                              @RequestParam("adminId") Integer adminId,
                              @RequestParam(value = "roleIdList", required = false) List<Integer> roleIdList) {
        System.out.println(roleIdList);
        adminService.deleteOldRelationship(adminId);
        adminService.insertNewRelationship(adminId, roleIdList);
        return "redirect:/admin/to/page.html?keyword=" + keyword + "&pageNum=" + pageNum;
    }

    @RequestMapping("/assign/get/all/auth.json")
    @ResponseBody
    public String getAllAuth() {
        List<Auth> auths = authService.getAllAuth();

        if (auths != null) {
            ResultEntity<List<Auth>> listResultEntity = ResultEntity.successWithData(auths);
            return gson.toJson(listResultEntity);
        } else {
            return gson.toJson(ResultEntity.failed("操作失败"));
        }
    }

    @RequestMapping("/assign/get/all/authId.json")
    @ResponseBody
    public String getAuthId(@RequestParam("roleId") Integer roleId) {
        List<Integer> roleIdList = authService.getAllAuthByRoleId(roleId);
        if (roleIdList == null || roleIdList.size() == 0) {
            return gson.toJson(ResultEntity.failed("操作失败"));
        }
        return gson.toJson(ResultEntity.successWithData(roleIdList));
    }

    @RequestMapping("/edit/to/auth/page.json")
    @ResponseBody
    public String editRoleAuth(@RequestBody Map<String, List<Integer>> map) {

         authService.insertAuth(map);
        return gson.toJson(ResultEntity.successWithOutData());
    }


}
